#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"	 
#include "24cxx.h"
#include "w25qxx.h"
#include "touch.h"
 
/************************************************
 ALIENTEK战舰STM32开发板实验27
 触摸屏 实验 
 技术支持：www.openedv.com
 淘宝店铺：http://eboard.taobao.com 
 关注微信公众平台微信号："正点原子"，免费获取STM32资料。
 广州市星翼电子科技有限公司  
 作者：正点原子 @ALIENTEK
************************************************/

typedef struct
{
  uint16_t TouchDetected;
  uint16_t X;
  uint16_t Y;
}TS_STATE; 

//清空屏幕并在右上角显示"RST"
void Load_Drow_Dialog(void)
{
	LCD_Clear(WHITE);	//清屏   
 	POINT_COLOR=BLUE;	//设置字体为蓝色 
	LCD_ShowString(lcddev.width-24,0,200,16,16,"RST");//显示清屏区域
  	POINT_COLOR=RED;	//设置画笔蓝色 
}
////////////////////////////////////////////////////////////////////////////////
//电容触摸屏专有部分
//画水平线
//x0,y0:坐标
//len:线长度
//color:颜色
void gui_draw_hline(u16 x0,u16 y0,u16 len,u16 color)
{
	if(len==0)return;
	LCD_Fill(x0,y0,x0+len-1,y0,color);	
}
//画实心圆
//x0,y0:坐标
//r:半径
//color:颜色
void gui_fill_circle(u16 x0,u16 y0,u16 r,u16 color)
{											  
	u32 i;
	u32 imax = ((u32)r*707)/1000+1;
	u32 sqmax = (u32)r*(u32)r+(u32)r/2;
	u32 x=r;
	gui_draw_hline(x0-r,y0,2*r,color);
	for (i=1;i<=imax;i++) 
	{
		if ((i*i+x*x)>sqmax)// draw lines from outside  
		{
 			if (x>imax) 
			{
				gui_draw_hline (x0-i+1,y0+x,2*(i-1),color);
				gui_draw_hline (x0-i+1,y0-x,2*(i-1),color);
			}
			x--;
		}
		// draw lines from inside (center)  
		gui_draw_hline(x0-x,y0+i,2*x,color);
		gui_draw_hline(x0-x,y0-i,2*x,color);
	}
}  
//两个数之差的绝对值 
//x1,x2：需取差值的两个数
//返回值：|x1-x2|
u16 my_abs(u16 x1,u16 x2)
{			 
	if(x1>x2)return x1-x2;
	else return x2-x1;
}  
//画一条粗线
//(x1,y1),(x2,y2):线条的起始坐标
//size：线条的粗细程度
//color：线条的颜色
void lcd_draw_bline(u16 x1, u16 y1, u16 x2, u16 y2,u8 size,u16 color)
{
	u16 t; 
	int xerr=0,yerr=0,delta_x,delta_y,distance; 
	int incx,incy,uRow,uCol; 
	if(x1<size|| x2<size||y1<size|| y2<size)return; 
	delta_x=x2-x1; //计算坐标增量 
	delta_y=y2-y1; 
	uRow=x1; 
	uCol=y1; 
	if(delta_x>0)incx=1; //设置单步方向 
	else if(delta_x==0)incx=0;//垂直线 
	else {incx=-1;delta_x=-delta_x;} 
	if(delta_y>0)incy=1; 
	else if(delta_y==0)incy=0;//水平线 
	else{incy=-1;delta_y=-delta_y;} 
	if( delta_x>delta_y)distance=delta_x; //选取基本增量坐标轴 
	else distance=delta_y; 
	for(t=0;t<=distance+1;t++ )//画线输出 
	{  
		gui_fill_circle(uRow,uCol,size,color);//画点 
		xerr+=delta_x ; 
		yerr+=delta_y ; 
		if(xerr>distance) 
		{ 
			xerr-=distance; 
			uRow+=incx; 
		} 
		if(yerr>distance) 
		{ 
			yerr-=distance; 
			uCol+=incy; 
		} 
	}  
}   
////////////////////////////////////////////////////////////////////////////////
 //5个触控点的颜色												 
const u16 POINT_COLOR_TBL[CT_MAX_TOUCH]={RED,GREEN,BLUE,BROWN,GRED};  
//电阻触摸屏测试函数
void rtp_test(void)
{
	u8 key;
	u8 i=0;	  
	while(1)
	{
	 	key=KEY_Scan(0);
		tp_dev.scan(0); 		 
		if(tp_dev.sta&TP_PRES_DOWN)			//触摸屏被按下
		{	
		 	if(tp_dev.x[0]<lcddev.width&&tp_dev.y[0]<lcddev.height)
			{	
				if(tp_dev.x[0]>(lcddev.width-24)&&tp_dev.y[0]<16)Load_Drow_Dialog();//清除
				else TP_Draw_Big_Point(tp_dev.x[0],tp_dev.y[0],RED);		//画图	  			   
			}
		}else delay_ms(10);	//没有按键按下的时候 	    
		if(key==KEY0_PRES)	//KEY0按下,则执行校准程序
		{
			LCD_Clear(WHITE);//清屏
		    TP_Adjust();  	//屏幕校准  
			Load_Drow_Dialog();
		}
		i++;
		if(i%20==0)LED0=!LED0;
	}
}
//电容触摸屏测试函数
void ctp_test(void)
{
	u8 t=0;
	u8 i=0;	  	    
 	u16 lastpos[5][2];		//记录最后一次的数据 
	while(1)
	{
		tp_dev.scan(0);
		for(t=0;t<CT_MAX_TOUCH;t++)
		{
			if((tp_dev.sta)&(1<<t))
			{
				if(tp_dev.x[t]<lcddev.width&&tp_dev.y[t]<lcddev.height)
				{
					if(lastpos[t][0]==0XFFFF)
					{
						lastpos[t][0] = tp_dev.x[t];
						lastpos[t][1] = tp_dev.y[t];
					}
					lcd_draw_bline(lastpos[t][0],lastpos[t][1],tp_dev.x[t],tp_dev.y[t],2,POINT_COLOR_TBL[t]);//画线
					lastpos[t][0]=tp_dev.x[t];
					lastpos[t][1]=tp_dev.y[t];
					if(tp_dev.x[t]>(lcddev.width-24)&&tp_dev.y[t]<16)
					{
						Load_Drow_Dialog();//清除
					}
				}
			}else lastpos[t][0]=0XFFFF;
		}
		
		delay_ms(5);i++;
		if(i%20==0)LED0=!LED0;
	}	
}

TS_STATE *IOE_TS_GetState(void)
{
	u8 key;
	u8 i = 0;	  
static	TS_STATE TS_State;
	TS_State.TouchDetected = 0;
	while( 1 )
	{	 	
		tp_dev.scan( 0 ); 		 
		if( tp_dev.sta & TP_PRES_DOWN )			//触摸屏被按下
		{	
		 	if( ( tp_dev.x[0] < lcddev.width ) && ( tp_dev.y[0] < lcddev.height ) )
			{	
				TS_State.TouchDetected = TP_PRES_DOWN;
				TS_State.X = tp_dev.x[0];// _x;
				/* Update the Y position */  
				TS_State.Y = tp_dev.y[0];// _y;	
				return &TS_State;					   
			}
		}else 
		delay_ms(10);	//没有按键按下的时候 	    				
	}
	return &TS_State;
}

 int main(void)
 {	 
 	u8 key;
 	u32 i;		    
	delay_init();	    	 //延时函数初始化	  
	

  	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2：2位抢占优先级，2位响应优先级
	uart_init( 115200 );
	LED_Init();
	LCD_Init();	
 	tp_dev.init();

POINT_COLOR = RED;//设置字体为红色
 	/* Draw a rectangle with the specifies parameters and Blue Color */
	//LCD_SetTextColor(Red);
	

 	POINT_COLOR = RED;//设置字体为红色 
	LCD_ShowString( 60,		50, 	200,	16,	16,	"Ex.Gateway" );	
	LCD_ShowString( 60,		70, 	200,	16,	16,	"TOUCH TEST" );	
	LCD_ShowString( 60, 	90, 	200,	16,	16,	"su_tech@126.com" );
	LCD_ShowString( 60, 	110, 	200,	16,	16,	"2021/1/15" );
   	LCD_ShowString( 60, 	130, 	200,	16,	16,	"Press S2 to Adjust" );	
   	if( tp_dev.touchtype != 0XFF )
   		LCD_ShowString( 60, 130, 200,16, 16, "Press S2 to Adjust" );//电阻屏才显示
	//delay_ms( 1500 );
	i = 250;
	do{
		key = GPIO_ReadInputDataBit( GPIOB, GPIO_Pin_2 );
		if( key == 0 )	//KEY0按下,则执行校准程序
		{
			LCD_Clear( WHITE );//清屏
		    TP_Adjust();  	//屏幕校准  
		    break;
		}
		delay_ms( 10 );
		if( i )
			i--;
	}while( i );

	LCD_Clear( WHITE );
	LCD_DrawRectangle( 40, 20, 200, 80 );
	LCD_DrawRectangle( 40, 100, 200, 160 );
	LCD_DrawRectangle( 40, 180, 200, 240 );
	LCD_DrawRectangle( 40, 260, 200, 320 );
	/* Loop infinitely */
	while( 1 )
	{
		static TS_STATE* TS_State; 

		/* Update the structure with the current position of the Touch screen */
		TS_State = IOE_TS_GetState();  

		if ( ( TS_State->TouchDetected == TP_PRES_DOWN ) && ( TS_State->X > 40 ) && ( TS_State->X < 200 ) )
		{
		  if ( ( TS_State->Y > 260 ) && ( TS_State->Y < 320 ) )
		  { 
		  	GPIO_SetBits( GPIOB, GPIO_Pin_3 );
		  	GPIO_SetBits( GPIOB, GPIO_Pin_4 );
		    LCD_ShowString( 50, 290, 240, 16, 16, "LED1 LED2 OFF" );
		  }
		  else if ( ( TS_State->Y > 180 ) && ( TS_State->Y < 240 ) )
		  {		  	
		  	GPIO_ResetBits( GPIOB, GPIO_Pin_3 );
		  	GPIO_ResetBits( GPIOB, GPIO_Pin_4 );
		   	LCD_ShowString( 50, 210, 240, 16, 16, "LED1 LED2 ON" );
		  }
		  else if ( ( TS_State->Y > 100 ) && ( TS_State->Y < 160 ) )
		  {
		  	GPIO_ResetBits( GPIOB, GPIO_Pin_4 );
		    LCD_ShowString( 50, 130, 240, 16, 16, "LED2 ON" );
		  } 
		  else if ( ( TS_State->Y > 20 ) && ( TS_State->Y < 80 ) )
		  {
		  	GPIO_ResetBits( GPIOB, GPIO_Pin_3 );
		    LCD_ShowString( 50, 50, 240, 16, 16, "LED1 ON" );
		  }		  
		}
		else
		{
			
		}
	}


	//Load_Drow_Dialog();	 	
	if( tp_dev.touchtype & 0X80 )  ctp_test();	//电容屏测试
	else rtp_test(); 						//电阻屏测试
}


